<?php
namespace app\index\controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:POST,GET");

use app\common\controller\HomeBase;
use think\Db;

class Luntan extends HomeBase
{
	protected function _initialize(){
		parent::_initialize();
	}

	/**
	*帖子详情
	*@param  int  page 		  帖子回复分页的第几页
	*@param  int  pagelimite  帖子每页显示几条回复
	*@param  int  id   		  帖子id   
	**/
	public function postDetail()
	{
		//获取传过来的参数
		$id = $_GET['id'];
		// $id = 2;
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:5);
		//更新(1)帖子阅读量+1
		$reading = Db('article')->where('id',$id)->value('reading');
		$reading = $reading + 1;
		$readingUpdate = Db('article')->where('id',$id)->setField('reading', $reading);

		//查询(1)帖子内容并且判断是否审核
		$post = Db('article')->where('id',$id)->where('status',1)->select();
		// dump($post);
		// die;
			//将帖子图片进行反序列化
		$photo = unserialize($post[0]['photo']);

		$post[0]['photo'] = $photo;
		//帖子内容与标题转码
		$content = $post[0]['content'];
		$content1 = htmlspecialchars_decode($content);
		$post[0]['content'] = $content1;
		$title = $post[0]['title'];
		$title1 = htmlspecialchars_decode($title);
		$post[0]['title'] = $title1;
		
		//查询(2)发帖人信息
		$uid = $post[0]["uid"];
		$userNumber = $post[0]["is_user"];
		//判断前台用户还是管理员
		if($userNumber == 1){
			$userInfo = Db('user')->where('id',$uid)->select();
			$post[0]['headImg'] = $userInfo[0]['headImg'];
			$post[0]['userName'] = $userInfo[0]['name'];
			$post[0]['time'] = $userInfo[0]['applicationDate'];
		}else{
			$userInfo = Db('admin_user')->where('id',$uid)->select();
			$post[0]['headImg'] = $userInfo[0]['headImg'];
			$post[0]['userName'] = $userInfo[0]['username'];
			$post[0]['time'] = $userInfo[0]['create_time'];
		}
		// dump($userInfo);
		// die();
		//将用户信息放入帖子数组中
		// $post[0]['headImg'] = $userInfo[0]['headImg'];
		// $post[0]['userName'] = $userInfo[0]['name'];
		// $post[0]['time'] = $userInfo[0]['applicationDate'];

		//查询(3)帖子对应的回复(帖子分页考虑)
		$reply = Db('reply')->where('article_id',$id)->page($page,$pagelimit)->select();

		//查询(4)帖子总数并返回分页总数
		$replyCount = Db('reply')->where('article_id',$id)->count();
		$pagecount = ceil($replyCount/$pagelimit);

		//判断回复是否为空
		if(!count($reply) == 0){
			//查询(5)回复人信息
			$userList = Db('user')->select();
			$i=0;
			for (; $i < count($reply); $i++) { 
				$userid = $reply[$i]['user_id'];
				//遍历用户列表数组
				$j = 0;
				for(;$j<count($userList);$j++){
					if($userid == $userList[$j]['id']){
						break;
					}
				}
				$userArr = Db('user')->where('id',$userid)->select();
				$reply[$i]['headImg'] = $userList[$j]['headImg'];
				$reply[$i]['userName'] = $userList[$j]['name'];
				$reply[$i]['time'] = $userList[$j]['applicationDate'];

				//转码回复内容
				$content = $reply[$i]['content'];
				$content2 = htmlspecialchars_decode($content);
				$reply[$i]['content'] = $content2;
			}

			foreach($reply as $v){
				//获取跟帖人的id
				$id = $v['id'];
				$after = Db('after')->where('reply_id',$id)->select();			
				//遍历取出跟帖人信息
				//取出所有的用户信息
				$userInfo = Db('user')->select();
				$i = 0;
				for (; $i < count($after); $i++) { 
					$userid = $after[$i]['after_userid'];
					//查询(6)跟帖人信息
					$j = 0;
					for(;$j<count($userInfo);$j++){
						if($userid == $userInfo[$j]['id']){
							break;
						}
					}
					//将跟帖人名字放入原来数组中
					$after[$i]['name'] = $userInfo[$j]['name'];
					
					//转码content
					$content = $after[$i]['content'];
					$content3 = htmlspecialchars_decode($content);
					$after[$i]['content'] = $content3;

				}
				// $replyInfo['reply'.$i] = $v;
				// $replyInfo['after'.$i] = $after;
				$v['after'] = $after;
				$replyInfo[]= $v;

				// $replyInfo[$i]['after'][]= $after;
			}
			//合并查询的两个数组
			// $postDetail = array_merge($post,$replyInfo);
			$postDetail['main'] = $post;
			$postDetail['child'] = $replyInfo;
			// $postDetail['pagecount'] = $pagecount;
			// dump($postDetail);
			$list['group'] = $postDetail;
			$list['pagecount'] = $pagecount;
			// dump($list);
			//判断
			if($list){
	            $this->apiSuccess($list);
	        }else{
	            $this->apiError('没有查到信息');
	        }
		}else{
			$postDetail['main'] = $post;
			// $postDetail['pagecount'] = $pagecount;
			// dump($postDetail);
			$list['group'] = $postDetail;
			$list['pagecount'] = $pagecount;
			dump($list);
			//判断
			if($list){
	            $this->apiSuccess($list);
	        }else{
	            $this->apiError('没有查到信息');
	        }
		}		
	}

	/**
	*给回复点赞
	*@param  int replyid 回复的id
	**/
	public function replyLike()
	{
		$replyid = $_GET['replyid'];
		//更新(1) 回复点赞数+1
		$likes = Db('reply')->where('id',$replyid)->value('likes');
		$likes = $likes + 1;
		$likeUpdate = Db('reply')->where('id',$replyid)->setField('likes', $likes);

		//判断
		if($likeUpdate){
            $this->apiSuccess('','点赞成功');
        }else{
            $this->apiError('点赞失败请重试');
        }
	}

	/**
	*给帖子点赞
	*@param  int postid 帖子的id
	**/
	public function postLike()
	{
		$postid = $_GET['postid'];
		//更新(1) 帖子点赞数+1
		$likes = Db('article')->where('id',$postid)->value('likes');
		$likes = $likes + 1;
		$likeUpdate = Db('article')->where('id',$postid)->setField('likes', $likes);

		//判断
		if($likeUpdate){
            $this->apiSuccess('','点赞成功');
        }else{
            $this->apiError('点赞失败请重试');
        }

	}

	/**
	*回帖
	*@param  char userid       input中上传用户id的名字
	*@param  int  postid       帖子的id
	**/
	public function replyAdd()
	{
		//获取表单传过来的用户id
		$userid = $_POST['userid'];
		//获取表单传过来文本内容
		$content = $_POST['text'];
		//将内容实体化
		$content = htmlspecialchars($content);
		//获取表单传过来帖子id
		$postid = $_POST['postid'];

		//查询帖子数组
		$postInfo = Db('article')->where('id',$postid)->select();
		//获取用户id ,栏目id ,判断前后体is_user
		$user_id = $userid;
		$category_id = $postInfo[0]['cid'];
		
		//获得添加时间
		$create_time = date('Y-m-d H:i:s',strtotime('now'));

		//获得所有数据添加数据库
		$data = [
			'category_id'=>$category_id,
			'article_id'=>$postid,
			'user_id'=>$user_id,
			'content'=>$content,
			'create_time'=>$create_time
		];
		$add = Db('reply')->insert($data);
		if($add){
			$list = Db('user')->where('id',$userid)->select();
			$list1['name'] = $list[0]['name'];
			$list1['headImg'] = $list[0]['headImg'];
			$list1['time'] = $create_time;
			$list1['content'] = $content;
			$this->apiSuccess($list1);
		}else{
			$this->apiError('发送失败,请重试');
		}


	}

	/**
	*给回复添加跟帖
	*@param  char $userid       input中上传用户id的名字
	*@param  int  $replyid      回复的id
	**/
	public function afterAdd()
	{
		//获取表单传过来的用户id
		$userid = $_POST['userid'];
		//获取表单传过来文本内容
		$content = $_POST['text'];
		//将内容序列化
		$content = htmlspecialchars($content);
		//获取表单传过来帖子id
		$replyid = $_POST['replyid'];

		//查询帖子数组
		$replyInfo = Db('reply')->where('id',$replyid)->select();
		//获取回帖数组中的信息 
		$category_id = $replyInfo[0]['category_id'];
		$after_userid = $userid;
		$reply_id = $replyid;
		$article_id = $replyInfo[0]['article_id'];
		$user_id = $replyInfo[0]['user_id'];
		
		//获得添加时间
		$create_time = date('Y-m-d H:i:s',strtotime('now'));
		//获得所有数据添加数据库
		$data = [
			'after_userid'=>$after_userid,
			'reply_id'=>$reply_id, 
			'category_id'=>$category_id,
			'article_id'=>$article_id,
			'user_id'=>$user_id,
			'content'=>$content,
			'create_time'=>$create_time
		];
		$add = Db('after')->insert($data);
		if($add){
			$list = Db('user')->where('id',$userid)->select();
			$list1['name'] = $list[0]['name'];
			$list1['headImg'] = $list[0]['headImg'];
			$list1['time'] = $create_time;
			$list1['content'] = $content;
			$this->apiSuccess($list1);
		}else{
			$this->apiError('发送失败,请重试');
		}
	}

}